var app = getApp()
Page({
  data: {
    devicenumber: '',
    display: 'none',
    items: [],
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    var that = this
    wx.request({
      url: "https://cd.tunnel.litelink.me/v1/smallapp/login",
      method: 'GET',
      data: {
        code: wx.getStorageSync('code'),
        sessionid: wx.getStorageSync('sessionid')
      },
      success: function (res) {
        console.log("------------------------------2", res)
        console.log("..........res.data.Datas1..........", res.data.Datas1)
        console.log("..........res.data.Datas2..........", res.data.Datas2)

        wx.setStorageSync('sessionid', res.data.Sessionids)
        wx.setStorageSync('openid', res.data.Openids)
        wx.setStorageSync('devicedata', res.data.Datas1)
        wx.setStorageSync('clientdata', res.data.Datas2)
      }
    }),
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 15000
      }),
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
  },

  onReady: function () {
    // 页面渲染完成
    var that = this
    wx.request({
      url: 'https://cd.tunnel.litelink.me/v1/smallapp/deviceList',
      data: {
        openid: wx.getStorageSync('openid')
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
         console.log("-res------", res)
        if (res.data != null&&res.statusCode=="200") {
          console.log("-------", res.data)
          that.setData({
            devicenumber: res.data.length,
            display: ''
          })
          var list = [];
          for (var i = 0; i < parseInt(res.data.length); i++) {
            list[i] = { "name": res.data[i].Name, "sn": res.data[i].SN, "id": res.data[i].ID }
          }
          wx.setStorageSync('devicename', list)
          console.log("name1.....", wx.getStorageSync('devicename'))
          that.setData({
            items: list,
          })
        } else if(res.statusCode=="404"){
          wx.showToast({
            title: '请稍后再试',
            icon: 'info',
            duration: 2000
          })
        }
      }
      ,
      fail:function(){
         wx.showToast({
            title: '网络故障，请稍后再试',
            icon: 'info',
            duration: 2000
          })
      }
    })
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  skip: function (event) {
    var len = wx.getStorageSync('devicename').length
    console.log(wx.getStorageSync('sessionid'), "------------", wx.getStorageSync('devicename'))

    for (var i = 0; i < len; i++)

      if (wx.getStorageSync('devicename')[i].name && event.target.dataset.alphaBeta == i) {

        app.globalData.SN = wx.getStorageSync('devicename')[i].sn
        app.globalData.deviceid = wx.getStorageSync('devicename')[i].id
        wx.navigateTo({
          url: '/pages/index/index?sessionid=' + wx.getStorageSync('sessionid') + '&devicename=' + wx.getStorageSync('devicename')[i].name + '&devicesn=' + wx.getStorageSync('devicename')[i].sn
        })
      }
  }
})